#include "StdAfx.h"
#include "pickearth.h"
#include <osgViewer/View>
#include <osgEarth/StringUtils>
#include <iostream>
#include "GlobelSetting.h" 
using namespace osg;
pickearth::pickearth(osgEarth::MapNode *mapnode):isSetEvent(false),isHandleClick(false),Mapnode(mapnode)
{

}
bool pickearth::handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &us)
{

	bool  handleb  =osgEarth::Util::EarthManipulator::handle(ea,us);
	if(  ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN &&ea.getKey()  == osgGA::GUIEventAdapter::KEY_1 )
	{
		osg::ref_ptr<osgEarth::Symbology::Geometry> value  =	GlobelSetting::GetInstance()->GetGlobelGeometry();
		if(value.valid())
		{

		}
	}
	 
	if(ea.getEventType()== osgGA::GUIEventAdapter::KEYDOWN &&  ea.getKey() == osgGA::GUIEventAdapter::KEY_5)
	{
		osgEarth::Viewpoint  viewp(getViewpoint());
		viewp.x()  = 115.28;
		viewp.y() =38.53;
		setViewpoint(viewp,2);
	}
	if(  ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN &&ea.getKey()  == osgGA::GUIEventAdapter::KEY_4 )
	{
		osg::ref_ptr<osgEarth::Symbology::Geometry> value  =	GlobelSetting::GetInstance()->GetGlobelGeometry();

	}
	if(  ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN &&ea.getKey()  == osgGA::GUIEventAdapter::KEY_3 )
	{
		osgViewer::View* view = dynamic_cast<osgViewer::View*>(&us);
		if(isSetEvent)
		{
			view->removeEventHandler(GlobelSetting::GetInstance()->GetGlobelAddPointHandler());

		}
		else

		{
			view->addEventHandler(GlobelSetting::GetInstance()->GetGlobelAddPointHandler());
		}
		isSetEvent  = !isSetEvent;
	}
 
	if(  ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN &&ea.getKey()  == osgGA::GUIEventAdapter::KEY_2 )
	{
		osg::ref_ptr<osgEarth::Symbology::Geometry> value  =	GlobelSetting::GetInstance()->GetGlobelGeometry();
		if(value.valid())
		{
			if(!value.get()->empty())
			{
				if( value.get()->size()  <= 3)
				{
					value.get()->clear();
				}
				else
				{
					value.get()->pop_back();
				}

				GlobelSetting::GetInstance()->GetGlobelFeatureModelGraph()->dirty();
			}
		}
	}
	//if ( ea.getEventType() == osgGA::GUIEventAdapter::MOVE )


	return  handleb;
}

pickearth::~pickearth(void)
{
} 